﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Clinica_Frba
{
    public partial class FormBusquedaAfiliadoParaEliminar : FormBusquedaAfiliado
    {
        public FormBusquedaAfiliadoParaEliminar()
        {
            InitializeComponent();
        }

        override protected void lBuscar_Click(object sender, EventArgs e)
        {
            bool bHuboWhere = false;
            string strQuery = "SELECT nombre, apellido, nro_doc , id_afil FROM AFILIADO WHERE ESTADO = 'H' AND ";

            if (tbNombre.Text.Length > 0)
            {
                strQuery += ("NOMBRE = '" + tbNombre.Text + "'");
                bHuboWhere = true;
            }

            if (tbApellido.Text.Length > 0)
            {
                if (bHuboWhere)
                    strQuery += " AND ";
                strQuery += ("APELLIDO = '" + tbApellido.Text + "'");
                bHuboWhere = true;
            }

            if (tbNroDeAfiliado.Text.Length > 0)
            {
                if (bHuboWhere)
                    strQuery += " AND ";
                strQuery += ("ID_AFIL = '" + tbNroDeAfiliado.Text + "'");
                bHuboWhere = true;
            }

            if (tbNroDeDocumento.Text.Length > 0)
            {
                if (bHuboWhere)
                    strQuery += " AND ";
                strQuery += ("NRO_DOC= '" + tbNroDeDocumento.Text + "'");
                bHuboWhere = true;
            }

            if (!bHuboWhere)
            {
                strQuery = "SELECT nombre, apellido, nro_doc , id_afil FROM AFILIADO WHERE ESTADO = 'H' ORDER BY nombre";
            }

            SqlConnection myConnection = new SqlConnection(publico.ConnectionString);
            myConnection.Open();
            SqlCommand consulta = new SqlCommand(strQuery, myConnection);
            SqlDataReader executeCMD = consulta.ExecuteReader();

            dgvResultadoDeBusqueda.Rows.Clear();
            while (executeCMD.Read())
            {
                dgvResultadoDeBusqueda.Rows.Add(executeCMD["nombre"], executeCMD["Apellido"], executeCMD["NRO_DOC"], executeCMD["ID_AFIL"]);
            }

            if (dgvResultadoDeBusqueda.Rows.Count == 0)
            {
                Dialogos.MostrarMessageDialog("No se encontraron coincidencias...");
            }

        }

        override protected void dgvResultadoDeBusqueda_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == dgvResultadoDeBusqueda.Columns["Seleccionar"].Index)
            {
                if (e.RowIndex >= 0)
                {
                    if (Dialogos.MostrarYesNoDialog("Seguro que quiere dar de baja a este afiliado ?"))
                    {
                        SqlConnection myConnection = new SqlConnection(publico.ConnectionString);
                        myConnection.Open();

                        string strAfiliadoID = dgvResultadoDeBusqueda.Rows[e.RowIndex].Cells["NroAfiliado"].Value.ToString();
                        string strFamiliaID = strAfiliadoID.Substring(0, 8);

                        string strQuery = "UPDATE AFILIADO SET ESTADO = 'I', FECHA_INHABILITACION = CONVERT(DATETIME, '" + DateTime.Today.ToString("yyyy-dd-MM") + "',103) WHERE ID_AFIL = '" + strAfiliadoID + "';";
                        strQuery += "UPDATE TURNO SET ESTADO = 'A' WHERE ID_AFIL = '" + strAfiliadoID + "';";
                        strQuery += "UPDATE RECETA SET ESTADO = 'I' WHERE ID_AFIL = '" + strAfiliadoID + "';"; 
                        strQuery = "BEGIN TRANSACTION;" + strQuery + "COMMIT TRANSACTION;";

                        SqlCommand sqlCommand = new SqlCommand(strQuery, myConnection);
                        int executeCMD = sqlCommand.ExecuteNonQuery();

                        dgvResultadoDeBusqueda.Rows.Clear();

                        if (executeCMD > 0 )
                            Dialogos.MostrarMessageDialog("Baja Satisfactoria");
                    }

                }

            }
        }

    }
}
